package com.example.springboot.service.impl;

import com.example.springboot.dao.RoleMapper;
import com.example.springboot.domain.*;
import com.example.springboot.service.RoleService;
import com.example.springboot.utils.SqlString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;


    @Override
    public List<Role> findAllRole(Role role) {
        List<Role> allRole = roleMapper.findAllRole(role);
        return allRole;
    }

    @Override
    public List<Integer> findMenuByRoleId(Integer roleid) {
        List<Integer> menuByRoleId = roleMapper.findMenuByRoleId(roleid);

        return menuByRoleId;
    }

    @Override
    public void roleContextMenu(RoleMenuVo roleMenuVo) {

        //1. 清空中间表的关联关系
        roleMapper.deleteRoleContextMenu(roleMenuVo.getRoleId());

        //2. 为角色分配菜单

        for (Integer mid : roleMenuVo.getMenuIdList()) {

            Role_menu_relation role_menu_relation = new Role_menu_relation();
            role_menu_relation.setMenuId(mid);
            role_menu_relation.setRoleId(roleMenuVo.getRoleId());

            //封装数据
            Date date = new Date();
            role_menu_relation.setCreatedTime(date);
            role_menu_relation.setUpdatedTime(date);

            role_menu_relation.setCreatedBy("system");
            role_menu_relation.setUpdatedby("system");


            roleMapper.roleContextMenu(role_menu_relation);
        }

    }

    @Override
    public void deleteRole(Integer roleid) {

        // 调用根据roleid清空中间表关联关系
        roleMapper.deleteRoleContextMenu(roleid);
        roleMapper.deleteRoleContextResource(roleid);
        roleMapper.deleteRole(roleid);
    }

    @Override
    public List<Integer> findResourceByRoleId(Integer resourceId) {
        List<Integer> resourceByRoleId=roleMapper.findResourceByRoleId(resourceId);
        return resourceByRoleId;
    }

    @Override
    public void roleContextResource(RoleResourceVo roleResourceVo) {
        //清空中间表关系
        roleMapper.deleteRoleContextResource(roleResourceVo.getRoleId());

        //为角色分配资源
        for (Integer rid : roleResourceVo.getResourceIdList()) {

            Role_resource_relation role_resource_relation=new Role_resource_relation();
            role_resource_relation.setResourceId(rid);
            role_resource_relation.setRoleId(roleResourceVo.getRoleId());

            //封装数据
            Date date=new Date();
            role_resource_relation.setCreatedTime(date);
            role_resource_relation.setUpdatedTime(date);

            role_resource_relation.setCreatedBy("system");
            role_resource_relation.setUpdatedby("system");

            roleMapper.roleContextResource(role_resource_relation);
        }

    }

    @Override
    public void addRole(Role role) {
        Date date = new Date();
        role.setUpdatedTime(date);
        role.setCreatedTime(date);
        role.setCreatedBy("system");
        role.setUpdatedBy("system");
        roleMapper.addRole(role);
    }

    @Override
    public void updateRole(Role role) {
        Date date = new Date();
        role.setUpdatedTime(date);
        roleMapper.updateRole(role);
    }
}
